﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Plumbing.ParamForm
{
	public partial class CopyForm : System.Windows.Forms.Form
	{
		public CopyForm(TreeNode selNode, InBuildSystem buildSystem)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.selNode = selNode;
			this.buildSystem = buildSystem;
			this.iStartNum = int.Parse(selNode.Text.Substring(selNode.Text.Length - 1, 1));
		}

		private void CopyForm_Load(object sender, EventArgs e)
		{
			object selectClass = new DataHelper(this.selNode, this.buildSystem).GetSelectClass();
			if (selectClass is ShareRPipeSystem)
			{
				this.Text = "复制共用立管数据";
			}
			else if (selectClass is FloorSystem)
			{
				this.Text = "复制楼层数据";
			}
			else if (selectClass is IndoorSystem)
			{
				this.Text = "复制户内数据";
			}
			else if (selectClass is CirclePipe)
			{
				this.Text = "复制环路数据";
			}
			this.hyTxtStartNum.Text = this.iStartNum.ToString();
			this.hyTxtEndNum.Text = (this.iStartNum + 1).ToString();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.hyTxtEndNum.Text == "")
			{
				YJKMessageBox.Information("参数不能为空。");
				this.hyTxtEndNum.Focus();
				return;
			}
			if (this.CopyData())
			{
				base.DialogResult = DialogResult.OK;
			}
		}

		private bool CopyData()
		{
			int num = int.Parse(this.hyTxtEndNum.Text);
			int num2 = num - this.iStartNum;
			DataHelper dataHelper = new DataHelper(this.selNode, this.buildSystem);
			object selectClass = dataHelper.GetSelectClass();
			object selectClassList = dataHelper.GetSelectClassList();
			if (this.chxCopy.Checked)
			{
				if (int.Parse(this.hyTxtStartNum.Text) >= int.Parse(this.hyTxtEndNum.Text))
				{
					YJKMessageBox.Information("后一个编号应大于前一个编号。");
					this.hyTxtEndNum.Focus();
					return false;
				}
				switch (this.selNode.Level)
				{
				case 1:
				{
					IList<ShareRPipeSystem> srpSystems = this.buildSystem.SrpSystems;
					int num3 = num - srpSystems.Count;
					for (int i = 0; i < num2; i++)
					{
						if (srpSystems.Count > this.iStartNum + i)
						{
							srpSystems.RemoveAt(this.iStartNum + i);
						}
						srpSystems.Insert(this.iStartNum - 1, new ShareRPipeSystem(selectClass as ShareRPipeSystem));
					}
					for (int j = 0; j < num3; j++)
					{
						dataHelper.CreateTotalPipe();
					}
					break;
				}
				case 2:
				{
					IList<FloorSystem> list = (IList<FloorSystem>)selectClassList;
					int num3 = num - list.Count;
					for (int k = 0; k < num2; k++)
					{
						if (list.Count > this.iStartNum + k)
						{
							list.RemoveAt(this.iStartNum + k);
						}
						list.Insert(this.iStartNum - 1, new FloorSystem(selectClass as FloorSystem));
					}
					for (int l = 0; l < num3; l++)
					{
						dataHelper.CreateRiserPipe();
					}
					break;
				}
				case 3:
				{
					IList<IndoorSystem> list2 = (IList<IndoorSystem>)selectClassList;
					for (int m = 0; m < num2; m++)
					{
						if (list2.Count > this.iStartNum + m)
						{
							list2.RemoveAt(this.iStartNum + m);
						}
						list2.Insert(this.iStartNum - 1, new IndoorSystem(selectClass as IndoorSystem));
					}
					break;
				}
				case 4:
				{
					IList<CirclePipe> list3 = (IList<CirclePipe>)selectClassList;
					for (int n = 0; n < num2; n++)
					{
						if (list3.Count > this.iStartNum + n)
						{
							list3.RemoveAt(this.iStartNum + n);
						}
						list3.Insert(this.iStartNum - 1, new CirclePipe(selectClass as CirclePipe));
					}
					break;
				}
				}
			}
			else
			{
				switch (this.selNode.Level)
				{
				case 1:
				{
					IList<ShareRPipeSystem> srpSystems2 = this.buildSystem.SrpSystems;
					int num3 = num - srpSystems2.Count;
					if (!this.SingleCopy<ShareRPipeSystem>(srpSystems2))
					{
						return false;
					}
					for (int num4 = 0; num4 < num3; num4++)
					{
						dataHelper.CreateTotalPipe();
					}
					srpSystems2.Insert(num - 1, new ShareRPipeSystem(selectClass as ShareRPipeSystem));
					break;
				}
				case 2:
				{
					IList<FloorSystem> list4 = (IList<FloorSystem>)selectClassList;
					int num3 = num - list4.Count;
					if (!this.SingleCopy<FloorSystem>(list4))
					{
						return false;
					}
					for (int num5 = 0; num5 < num3; num5++)
					{
						dataHelper.CreateRiserPipe();
					}
					list4.Insert(num - 1, new FloorSystem(selectClass as FloorSystem));
					break;
				}
				case 3:
				{
					IList<IndoorSystem> list5 = (IList<IndoorSystem>)selectClassList;
					if (!this.SingleCopy<IndoorSystem>(list5))
					{
						return false;
					}
					list5.Insert(num - 1, new IndoorSystem(selectClass as IndoorSystem));
					break;
				}
				case 4:
				{
					IList<CirclePipe> list6 = (IList<CirclePipe>)selectClassList;
					if (!this.SingleCopy<CirclePipe>(list6))
					{
						return false;
					}
					list6.Insert(num - 1, new CirclePipe(selectClass as CirclePipe));
					break;
				}
				}
			}
			return true;
		}

		private bool SingleCopy<T>(IList<T> listSystem)
		{
			int num = int.Parse(this.hyTxtEndNum.Text);
			if (listSystem.Count + 1 < num)
			{
				YJKMessageBox.Information("数据输入范围：1~" + (listSystem.Count + 1));
				this.hyTxtEndNum.Focus();
				return false;
			}
			if (listSystem.Count >= num)
			{
				listSystem.RemoveAt(num - 1);
			}
			return true;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private TreeNode selNode;

		private InBuildSystem buildSystem;

		private int iStartNum;
	}
}
